<?php 
/* This script uses the minifier to provide a single 
 * minified package of either css or javascript files.
 * 
 * That's why it is called min pack. Because it packages
 * the files required into one pack and the minifies them.
 */

// the global configuration file
require_once("config.php");

// external minification classes
require_once("lib/Minify/JSMin.class.php");
require_once("lib/Minify/Minify_HTML.class.php");
require_once("lib/Minify/Minify_Javascript.class.php");

// the internal minification class
require_once("class/minify.class.php");
require_once("class/database.class.php");
require_once("class/blog.class.php");

// instanciate the blog
$b = new blog($_SERVER["HTTP_HOST"]);

/* output gzip enable, when gzip is accepted */
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
	// client accepts gzip, so gzip the content
	ob_start("ob_gzhandler");
}else{ 
	// continue normally when gzip is unsupported
	ob_start();
}

// define the required java script files
$js_pack = explode(";", CONFIG_MINPACK_JS);
$css_pack = explode(";", CONFIG_MINPACK_CSS);

/* check if the required param exists
 * that defines the type of pack (css or js) */
if(array_key_exists("t", $_GET)){
	// get the requested pack type
	$pack_type = $_GET["t"];
	
	// check if js pack is requested
	if($pack_type=="js"){
		// will hold all script code
		$js_content = "";
		
		// walk through all js
		// files and pack them
		for($j=0;$j<count($js_pack);$j++){
			// fill the package
			$js_content .= file_get_contents(CONFIG_ROOTDIR.$js_pack[$j])."\n";
		}
		
		// safely minify the javascript
		$js_content = minify::minifyJS($js_content);
		
		// set the expires header to enforce caching
		header("Cache-Control: max-age=31556926");
		header('Date: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
		header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
		header("Expires: ".gmdate('D, d M Y H:i:s \G\M\T',(time()+31556926)));
		
		// set the correct header for this js pack
		header("Content-Type: text/javascript");
		
		// finally output the minpack
		print($js_content);
		
	}// check if js pack is requested
	if($pack_type=="css"){
		// will hold all script code
		$css_content = "";
		
		// walk through all js
		// files and pack them
		for($c=0;$c<count($css_pack);$c++){
			// fill the package
			$css_content .= file_get_contents(CONFIG_ROOTDIR.$css_pack[$c])."\n";
		}
		
		// change the host name for the images in the css file
		// so that they use the available CDN hosts for delivery
		$hostlist = explode(";", CONFIG_CDN_IMG_DOMAIN);
		preg_match_all("|url\(/([^>]*)\);|U", $css_content, $found);

		if(count($found)>0){
			$h = 0;
			for($i=0;$i<count($found[0]);$i++){
				if($h>=count($hostlist)){
					$h=0;
				}
				
				$oldVal = "url(/".$found[1][$i].")";
				$newVal = "url('http://".$hostlist[$h]."/".$found[1][$i]."')";
				$css_content = str_replace($oldVal, $newVal, $css_content);
				
				$h++;
			}
		}
		
		// minifiy the css package
		$css_minpack = minify::minifyCSS($css_content);
		
		// set the expires header to enforce caching
		header("Cache-Control: max-age=31556926");
		header('Date: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
		header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
		header("Expires: ".gmdate('D, d M Y H:i:s \G\M\T',(time()+31556926)));
		
		// set the correct header for this css pack
		header("Content-Type: text/css");
		
		// finally output the minpack
		print($css_minpack);
	}
	
}
?>